package com.mdd.admin.service.order.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.mdd.admin.service.order.IPreAuthorizationPaymentRecordService;
import com.mdd.admin.validate.order.PreAuthorizationPaymentRecordCreateValidate;
import com.mdd.admin.validate.order.PreAuthorizationPaymentRecordUpdateValidate;
import com.mdd.admin.vo.order.PreAuthorizationPaymentRecordVo;
import com.mdd.common.entity.order.PreAuthorizationPaymentRecord;
import com.mdd.common.mapper.order.PreAuthorizationPaymentRecordMapper;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.Assert;
import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

/**
 * 预授款扣款记录实现类
 * @author LikeAdmin
 */
@Service
public class PreAuthorizationPaymentRecordServiceImpl implements IPreAuthorizationPaymentRecordService {

    @Resource
    PreAuthorizationPaymentRecordMapper preAuthorizationPaymentRecordMapper;
    /**
     * 预授款扣款记录列表
     * @param orderId 订单编号
     */
    @Override
    public List<PreAuthorizationPaymentRecordVo> list(Long orderId) {
        LambdaQueryWrapper<PreAuthorizationPaymentRecord> paymentRecordWrapper=new LambdaQueryWrapper<>();
        paymentRecordWrapper.eq(PreAuthorizationPaymentRecord::getOrderId,orderId);
        List<PreAuthorizationPaymentRecord> paymentRecords = preAuthorizationPaymentRecordMapper.selectList(paymentRecordWrapper);
        List<PreAuthorizationPaymentRecordVo> paymentRecordVos=new ArrayList<>();
        paymentRecords.forEach(preAuthorizationPaymentRecord -> {
            PreAuthorizationPaymentRecordVo paymentRecordVo=new PreAuthorizationPaymentRecordVo();
            BeanUtils.copyProperties(preAuthorizationPaymentRecord,paymentRecordVo);
            paymentRecordVos.add(paymentRecordVo);
        });
        return paymentRecordVos;
    }

    /**
     * 预授款扣款记录新增
     *
     * @author LikeAdmin
     * @param createValidate 参数
     */
    @Transactional
    @Override
    public void add(PreAuthorizationPaymentRecordCreateValidate createValidate) {
        PreAuthorizationPaymentRecord model = new PreAuthorizationPaymentRecord();
        model.setPreAuthorizationId(createValidate.getPreAuthorizationId());
        model.setOperateType(createValidate.getOperateType());
        model.setMoney(createValidate.getMoney());
        model.setRemarks(createValidate.getRemarks());
        model.setAlipayNo(createValidate.getAlipayNo());
        model.setStatus(createValidate.getStatus());
        preAuthorizationPaymentRecordMapper.insert(model);
    }

    /**
     * 预授款扣款记录编辑
     *
     * @author LikeAdmin
     * @param updateValidate 参数
     */
    @Transactional
    @Override
    public void edit(PreAuthorizationPaymentRecordUpdateValidate updateValidate) {
        PreAuthorizationPaymentRecord model = preAuthorizationPaymentRecordMapper.selectById(updateValidate.getId());

        Assert.notNull(model, "预授款扣款记录数据不存在!");

        model.setId(updateValidate.getId());
        model.setPreAuthorizationId(updateValidate.getPreAuthorizationId());
        model.setOperateType(updateValidate.getOperateType());
        model.setMoney(updateValidate.getMoney());
        model.setRemarks(updateValidate.getRemarks());
        model.setAlipayNo(updateValidate.getAlipayNo());
        model.setStatus(updateValidate.getStatus());
        preAuthorizationPaymentRecordMapper.updateById(model);
    }

    /**
     * 预授款扣款记录删除
     *
     * @author LikeAdmin
     * @param id 主键ID
     */
    @Transactional
    @Override
    public void del(Long id) {
        PreAuthorizationPaymentRecord model = preAuthorizationPaymentRecordMapper.selectById(id);

        Assert.notNull(model, "预授款扣款记录数据不存在!");

        preAuthorizationPaymentRecordMapper.deleteById(id);
    }

}
